﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using TravelSite.DAL;
using TravelSite.DAL.Repositories;
using TravelSite.Entities;
using TravelSite.Code;
using TravelSite.Controls;

namespace TravelSite.Admin
{
    public partial class ContactManagementContracts : BaseDataPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ctrlDetails.ContactId = ContactId;
                using (var repo = RepositoryFactory.ResolveRepository<ContactRepository>())
                {
                    Contact cnt = repo.Find(ContactId);
                    if (cnt == null)
                        Response.Redirect("ContactManagement.aspx", true);
                    else
                        ctrlDetails.ContactName = cnt.FullName;
                }

                lblTitle1.Text = "Contacts > ";
                hlContactTitle.Text = ctrlDetails.ContactName;
                hlContactTitle.Attributes.Add("onclick", string.Format("createUpdateForm({0})", ContactId.ToString()));
                lblTitle2.Text = " > Contracts";
                LoadData();
            }

        }

        public override void LoadData()
        {
            using (var repo = RepositoryFactory.ResolveRepository<ContractRepository>())
            {
                grid.DataSource = repo.FindByContact(ContactId);
                grid.DataBind();
                upMain.Update();
            }
        }

        protected void grid_RowDataBound(Object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Contract item = (Contract)e.Row.DataItem;

                Label lbl = (Label)e.Row.FindControl("lblContractDate");
                if (item.ContractDate == null)
                    lbl.Text = "";
                else
                    lbl.Text = Convert.ToDateTime(item.ContractDate).ToString("d");

                HtmlAnchor lnk = (HtmlAnchor)e.Row.FindControl("lnkEdit");
                //lnk.Visible = (item.StatusValue == ContractStatus.Draft) || (item.StatusValue == ContractStatus.Sent);
                lnk.Attributes.Add("onclick", string.Format("javascript:createContractForm({0});", item.ContractId.ToString()));

                lnk = (HtmlAnchor)e.Row.FindControl("lnkCC");
                lnk.Attributes.Add("onclick", string.Format("javascript:showCCDialog({0});", item.ContractId.ToString()));

                ImageButton ibtn = (ImageButton)e.Row.FindControl("lbtnDelete");
                //ibtn.Visible = (item.StatusValue == ContractStatus.Draft) || (item.StatusValue == ContractStatus.Sent);

                lnk = (HtmlAnchor)e.Row.FindControl("lnkPdf");
                lnk.Visible = (item.StatusValue == ContractStatus.Sent) || (item.StatusValue == ContractStatus.Viewed) || (item.StatusValue == ContractStatus.Signed);
                lnk.HRef = string.Format("ContractToPdf.aspx?c={0}", item.ContractId.ToString());

                ibtn = (ImageButton)e.Row.FindControl("btnResend");
                ibtn.Visible = (item.StatusValue == ContractStatus.Sent) || (item.StatusValue == ContractStatus.Viewed);

                lnk = (HtmlAnchor)e.Row.FindControl("lnkVerify");
                //lnk.Visible = ((item.PackageStatus == "PENDING") || (item.PackageStatus == "BOOKED"));
                lnk.Visible = item.AllowVerify;
                lnk.Attributes.Add("onclick", string.Format("javascript:createVerifyForm({0});", item.ContractId.ToString()));
            }
        }

        protected void grid_Command(object sender, CommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument.ToString());

            using (var repo = RepositoryFactory.ResolveRepository<ContractRepository>())
            {
                switch (e.CommandName)
                {
                    case "delete":
                        repo.Delete(id);
                        break;
                    case "resend":
                        string result = ContractDetails.SendContract(id, Server.MapPath("~/Files/Templates"));
                        ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Alert", string.Format("alert('{0}');", result), true);
                        break;
                }
            }

            LoadData();
        }

        public int ContactId
        {
            get
            {
                int result = -1;
                if (!string.IsNullOrEmpty(Request.QueryString["c"]))
                    result = Convert.ToInt32(Request.QueryString["c"]);
                return result;
            }
        }
       
    }
}